﻿using System.Collections.Generic;
using System;
using Kingdee.CDP.WebApi.SDK;
using System.Text;
using System.Linq;
using Newtonsoft.Json.Linq;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using WJSHWebApi.InterFace;
using System.Threading.Tasks;
using WJSHWebApi.DBModel;
using System.Threading;

namespace WJSHWebApi.Server
{
    public class KingDeeServer: BackgroundService
    {
        IDBInterface iDBInterface;
        public KingDeeServer(IDBInterface dBInterface) 
        {
            iDBInterface=dBInterface;
        }
        public List<FHTZD> GetFHTZDs() 
        {
            List<FHTZD> fHTZDs = new List<FHTZD>();
            K3CloudApi client = new K3CloudApi();
            //用于记录结果
            StringBuilder Info = new StringBuilder();
            //业务对象标识
            string formId = "SAL_DELIVERYNOTICE";
            //查询字段集合，即返回哪些数据，不能为空，根据不同业务单据填写不同的字段名，以下仅为示例
            string fieldKeys = "FBILLNO,FDATE,FLOT";
            string filterString = $"FDATE='{DateTime.Now.Date.ToString()}' AND FDOCUMENTSTATUS='C' AND FStockID=129211";
            var param = new QueryParam()
            {
                FormId = formId,
                FieldKeys = fieldKeys,
                FilterString = filterString,
            };
            //124808
            //调用接口
            var result = client.ExecuteBillQuery(param.ToJson()).ToList();
            if (result.Count == 1)
            {
                var resultJObject = JArray.Parse(JsonConvert.SerializeObject(result[0]));
                var queryNode = resultJObject.SelectToken("$..IsSuccess");
                if (queryNode != null && !queryNode.Value<Boolean>())
                {
                    return null;
                }
                foreach (var item in result)
                {

                    if (!string.IsNullOrEmpty(item[2].ToString()))
                    {
                        var PH= GETPH(item[2].ToString());
                        if (PH != null)
                        {
                            fHTZDs.Add(new FHTZD() { FBILLNO = item[0].ToString(), FDATE = Convert.ToDateTime(item[1].ToString()), PHID = item[2].ToString(), PH = GETPH(item[2].ToString()) });
                        }
                        else 
                        {
                            fHTZDs.Add(new FHTZD() { FBILLNO = item[0].ToString(), FDATE = Convert.ToDateTime(item[1].ToString()), PHID = item[2].ToString() });
                        }
                      
                    }
                    else 
                    {
                        fHTZDs.Add(new FHTZD() { FBILLNO = item[0].ToString(), FDATE = Convert.ToDateTime(item[1].ToString()), PHID = item[2].ToString()});
                    }

                }

                return fHTZDs;
            }
            else
            {
                foreach (var item in result)
                {

                    if (!string.IsNullOrEmpty(item[2].ToString()))
                    {
                        var PH = GETPH(item[2].ToString());
                        if (PH != null)
                        {
                            fHTZDs.Add(new FHTZD() { FBILLNO = item[0].ToString(), FDATE = Convert.ToDateTime(item[1].ToString()), PHID = item[2].ToString(), PH = GETPH(item[2].ToString()) });
                        }
                        else
                        {
                            fHTZDs.Add(new FHTZD() { FBILLNO = item[0].ToString(), FDATE = Convert.ToDateTime(item[1].ToString()), PHID = item[2].ToString() });
                        }

                    }
                    else
                    {
                        fHTZDs.Add(new FHTZD() { FBILLNO = item[0].ToString(), FDATE = Convert.ToDateTime(item[1].ToString()), PHID = item[2].ToString() });
                    }
                }

                return fHTZDs;
            }
        }


        public string GETPH(string id)
        {
            K3CloudApi client = new K3CloudApi();
            StringBuilder Info = new StringBuilder();
            string formId = "BD_BatchMainFile";
            string fieldKeys = "FLOTID,FNUMBER";
            string filterString = $"FLOTID='{id}'";
            var param = new QueryParam()
            {
                FormId = formId,
                FieldKeys = fieldKeys,
                FilterString = filterString,
            };
            var result = client.ExecuteBillQuery(param.ToJson());
            if (result.Count == 1)
            {
                var resultJObject = JArray.Parse(JsonConvert.SerializeObject(result[0]));
                var queryNode = resultJObject.SelectToken("$..IsSuccess");
                if (queryNode != null && !queryNode.Value<Boolean>())
                {
                    return null;
                }
                else
                {
                    return result[0][1].ToString();
                }
            }
            else

                return null;
        }

        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {


              
                await Task.Delay(3000, stoppingToken);

            }
        }
    }


    public class FHTZD 
    {
        public string FBILLNO { get; set; }
        public DateTime FDATE { get; set; }
        public string PHID { get; set; }
        public string PH { get; set; }
    }
}
